package com.heyun.service.impl;

import com.heyun.entity.dto.GradesDTO;
import com.heyun.entity.pojo.PointsLog;
import com.heyun.entity.vo.*;
import com.heyun.mapper.StudentMapper;
import com.heyun.service.StudentService;
import com.heyun.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
//    @Autowired
//    private ThreadLocalUtil threadLocalUtil;
    @Override
    public SubjectVO[] getSubData() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();

        return studentMapper.getSubData(userId);
    }

    @Override
    public GradeVO[] getGrade(String id) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return studentMapper.getGrade(userId,id);
    }

    @Override
    public ExpVO[] getExp() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return studentMapper.getExp(userId);
    }

    @Override
    public Integer delExp(String expId) {
    return studentMapper.delExp(expId);
    }

    @Override
    public Homework[] getHomework() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        Homework[] homeworkOfStudent = studentMapper.getHomeworkOfStudent(userId);
        if (homeworkOfStudent.length >= 1)
            return homeworkOfStudent;
        return null;
    }

    @Override
    public GradesDTO[] getGrades() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return studentMapper.getGrades(userId);
    }

    @Override
    public int SignIn(String subjectId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        messagingTemplate.convertAndSend("/topic/hasSign", userId);
        studentMapper.updatePoints(userId, subjectId, 10.0);
        return studentMapper.SignIn(userId);
    }

    @Override
    public SignIn getSignIn() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return studentMapper.getSignIn(userId);
    }

    @Override
    public int addLog(PointsLog pl) {
        return studentMapper.addLog(pl);
    }

    @Override
    public StudentLogVO[] getLogs() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return studentMapper.getLogs(userId);
    }

    @Override
    public SignIn[] getSignIns() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userId = claims.get("id").toString();
        return  studentMapper.getSignIns(userId);
    }
}
